//
//  MyAdComboCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/22/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class MyAdComboCell: UITableViewCell, ConfigurableCell {
    
    private lazy var parentView: UIView = {
        let view = UIView()
        view.backgroundColor = .greyBlue
        view.layer.cornerRadius = 10
        return view
    }()
    
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 24, weight: .semibold)
        lbl.textColor = .darkBlueColor
        lbl.sizeToFit()
        lbl.numberOfLines = 1
        return lbl
    }()
    
    private lazy var hintLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 12, weight: .regular)
        lbl.textColor = .darkBlueColor
        lbl.numberOfLines = 0
        return lbl
    }()
    
    private lazy var optionsStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .leading
        stack.distribution = .fillEqually
        stack.spacing = 8
        return stack
    }()
    
    private lazy var priceWrapper: UIView = {
        let view = UIView()
        view.backgroundColor = .blueColor
        view.layer.cornerRadius = 10
        return view
    }()
    
    private lazy var priceLbl: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .white
        lbl.font = .systemFont(ofSize: 15, weight: .medium)
        lbl.sizeToFit()
        return lbl
    }()
    
    func configure(data: MyAdViewModel.Combo?, index: Int) {
        self.priceLbl.text = "\(data!.price) ₸"
        self.titleLbl.text = data?.name
        self.hintLbl.text = data?.description
        self.handleIcons(data: data!)
    }

    func returnData() -> MyAdViewModel.Combo? { return nil }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addViews () {
        parentView.addSubview(titleLbl)
        parentView.addSubview(hintLbl)
        parentView.addSubview(optionsStack)
        parentView.addSubview(priceWrapper)
        priceWrapper.addSubview(priceLbl)
        contentView.addSubview(parentView)
        
        parentView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(13)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        
        titleLbl.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview().inset(16)
            make.height.equalTo(30)
        }
        priceWrapper.snp.makeConstraints { (make) in
            make.bottom.trailing.equalToSuperview().inset(16)
            make.height.equalTo(32)
        }
        priceLbl.snp.makeConstraints { (make) in
            make.height.equalTo(22)
            make.top.bottom.equalToSuperview().inset(5)
            make.leading.trailing.equalToSuperview().inset(9)
        }
        hintLbl.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.leading.bottom.equalToSuperview().inset(16)
            //make.trailing.equalTo(priceWrapper.snp.leading).offset(-8)
            make.top.equalTo(titleLbl.snp.bottom).offset(12)
        }
        optionsStack.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLbl)
            make.leading.equalTo(titleLbl.snp.trailing).offset(16)
        }
    }
    
    private func handleIcons (data: MyAdViewModel.Combo) {
        _ = optionsStack.arrangedSubviews.map {
            optionsStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        if data.options.isTop {
            let icon = self.createIcon(iconName: "top")
            optionsStack.addArrangedSubview(icon)
        }
        if data.options.special {
            let icon = self.createIcon(iconName: "safe")
            optionsStack.addArrangedSubview(icon)
        }
        if data.options.searchUp {
            let icon = self.createIcon(iconName: "arrowUp")
            optionsStack.addArrangedSubview(icon)
        }
        if data.options.colored {
            let icon = self.createIcon(iconName: "paint")
            optionsStack.addArrangedSubview(icon)
        }
    }
    
    private func createIcon (iconName: String) -> UIImageView {
        let imageView = UIImageView()
        imageView.image = UIImage(named: iconName)
        imageView.contentMode = .scaleAspectFit
        imageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
        }
        return imageView
    }
}
